<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="昨晚正在进行伟大的学术讨论。我提了这么一个问题： 123a&#x3D;2;b&#x3D;++a+a++;cout&lt;&lt;b;  问此时 b 应该输出多少？ 按照正常的思维，计算的流程应当是这样的：a 先加一，此时 a&#x3D;3，然后 a 再加 a，赋值给 b，b&#x3D;6，最后 a 再加一，a&#x3D;4。 我觉得我算的没错，但是当我想要确认一下的时候，诡异的事情来了，计算结果为7。我又让他们用电脑算了一下，结果是6。相同的代码">
<meta property="og:type" content="article">
<meta property="og:title" content="g++和chang编译程序出现不同结果分析">
<meta property="og:url" content="http://example.com/2021/09/07/gcc-chang-different/index.html">
<meta property="og:site_name" content="Maicss">
<meta property="og:description" content="昨晚正在进行伟大的学术讨论。我提了这么一个问题： 123a&#x3D;2;b&#x3D;++a+a++;cout&lt;&lt;b;  问此时 b 应该输出多少？ 按照正常的思维，计算的流程应当是这样的：a 先加一，此时 a&#x3D;3，然后 a 再加 a，赋值给 b，b&#x3D;6，最后 a 再加一，a&#x3D;4。 我觉得我算的没错，但是当我想要确认一下的时候，诡异的事情来了，计算结果为7。我又让他们用电脑算了一下，结果是6。相同的代码">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-07T01:38:03.000Z">
<meta property="article:modified_time" content="2021-11-16T07:49:25.342Z">
<meta property="article:author" content="Maicss">
<meta property="article:tag" content="编译器">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/09/07/gcc-chang-different/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/09/07/gcc-chang-different/","path":"2021/09/07/gcc-chang-different/","title":"g++和chang编译程序出现不同结果分析"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>g++和chang编译程序出现不同结果分析 | Maicss</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Maicss</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Maicss</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">13</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/07/gcc-chang-different/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Maicss">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Maicss">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          g++和chang编译程序出现不同结果分析
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-09-07 09:38:03" itemprop="dateCreated datePublished" datetime="2021-09-07T09:38:03+08:00">2021-09-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-16 15:49:25" itemprop="dateModified" datetime="2021-11-16T15:49:25+08:00">2021-11-16</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>昨晚正在进行伟大的学术讨论。我提了这么一个问题：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a=<span class="number">2</span>;</span><br><span class="line">b=++a+a++;</span><br><span class="line">cout&lt;&lt;b;</span><br></pre></td></tr></table></figure>

<p>问此时 b 应该输出多少？</p>
<p>按照正常的思维，计算的流程应当是这样的：a 先加一，此时 a=3，然后 a 再加 a，赋值给 b，b=6，最后 a 再加一，a=4。</p>
<p>我觉得我算的没错，但是当我想要确认一下的时候，诡异的事情来了，计算结果为7。我又让他们用电脑算了一下，结果是6。相同的代码运行出了不同的结果，一般来说就是不同的编译器的实现不同了。当我把编译器从 gcc 换成 chang 后，结果就变成了6。因此，我想看看这两个编译器分别是怎么理解这种代码的。</p>
<p>首先对我认为结果正常的 chang 编译器编译的结果进行分析。</p>
<p>截取这一段的汇编代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># int a=2;</span><br><span class="line"># 将2赋给a，这里-0x8(%rbp)可以理解为变量a</span><br><span class="line">movl   $0x2,-0x8(%rbp) </span><br><span class="line"></span><br><span class="line"># int b=++a+a++;</span><br><span class="line"># 将变量a的值赋给寄存器eax</span><br><span class="line">mov    -0x8(%rbp),%eax</span><br><span class="line"># 将寄存器eax中的值加一</span><br><span class="line">add    $0x1,%eax</span><br><span class="line"># 将加一后的值重新赋给a</span><br><span class="line">mov    %eax,-0x8(%rbp)</span><br><span class="line"># 再将a的值赋给寄存器ecx</span><br><span class="line">mov    -0x8(%rbp),%ecx</span><br><span class="line"># 将ecx的值转移到edx</span><br><span class="line">mov    %ecx,%edx</span><br><span class="line"># 将edx加一</span><br><span class="line">add    $0x1,%edx</span><br><span class="line"># 将edx的值赋给a</span><br><span class="line">mov    %edx,-0x8(%rbp)</span><br><span class="line"># 将ecx和eax的值相加赋给eax</span><br><span class="line">add    %ecx,%eax</span><br><span class="line"># 将eax的值赋给b</span><br><span class="line">mov    %eax,-0xc(%rbp)</span><br></pre></td></tr></table></figure>

<p>可以看出，其实这段逻辑和我们计算的逻辑差不多。再来对比一下 gcc 是怎么实现的：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"># int a=2;</span><br><span class="line"># 将2赋给a</span><br><span class="line">movl   $0x2,-0x4(%rbp)</span><br><span class="line"></span><br><span class="line"># int b=++a+a++;</span><br><span class="line"># 将a加一</span><br><span class="line">addl   $0x1,-0x4(%rbp)</span><br><span class="line"># 将加一后的a存储到寄存器eax</span><br><span class="line">mov    -0x4(%rbp),%eax</span><br><span class="line"># 将a的值+1再存储到edx中</span><br><span class="line">lea    0x1(%rax),%edx</span><br><span class="line"># 将edx中的值赋给a</span><br><span class="line">mov    %edx,-0x4(%rbp)</span><br><span class="line"># 将a的值赋给edx</span><br><span class="line">mov    -0x4(%rbp),%edx</span><br><span class="line"># 将edx和eax的值相加并存到eax中</span><br><span class="line">add    %edx,%eax</span><br><span class="line"># 将eax的值给b</span><br><span class="line">mov    %eax,-0x8(%rbp)</span><br></pre></td></tr></table></figure>

<p>这段代码说实话感觉非常奇怪。有点不符合我们正常的思维方式。</p>
<p>因此，还是推荐使用clang作为编译器编译。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BC%96%E8%AF%91%E5%99%A8/" rel="tag"># 编译器</a>
              <a href="/tags/C/" rel="tag"># C++</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/03/ftp-server-config/" rel="prev" title="vsftpd服务器配置详细中文说明">
                  <i class="fa fa-chevron-left"></i> vsftpd服务器配置详细中文说明
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/09/fedora-fcitx5/" rel="next" title="在fedora34中安装fcitx5输入法替换ibus">
                  在fedora34中安装fcitx5输入法替换ibus <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Maicss</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
